"""
示例插件B - 数学计算插件
"""

import math

class MathCalculatorPlugin:
    """数学计算插件类"""
    
    def __init__(self):
        self.name = "MathCalculator"
        self.version = "1.0.0"
    
    def calculate(self, expression):
        """
        安全计算数学表达式
        
        Args:
            expression: 数学表达式字符串
        
        Returns:
            计算结果
        """
        try:
            # 安全的数学函数列表
            safe_functions = {
                'sin': math.sin,
                'cos': math.cos,
                'tan': math.tan,
                'sqrt': math.sqrt,
                'log': math.log,
                'abs': abs,
                'pow': pow,
                'round': round,
                'pi': math.pi,
                'e': math.e
            }
            
            # 创建安全的计算环境
            safe_dict = {"__builtins__": {}}
            safe_dict.update(safe_functions)
            
            result = eval(expression, safe_dict)
            return result
        except Exception as e:
            return f"计算错误: {str(e)}"
    
    def basic_stats(self, numbers):
        """
        计算基本统计信息
        
        Args:
            numbers: 数字列表
        
        Returns:
            统计信息字典
        """
        if not numbers:
            return None
        
        return {
            'count': len(numbers),
            'sum': sum(numbers),
            'mean': sum(numbers) / len(numbers),
            'min': min(numbers),
            'max': max(numbers),
            'range': max(numbers) - min(numbers)
        }
    
    def get_info(self):
        """获取插件信息"""
        return {
            "name": self.name,
            "version": self.version,
            "description": "数学计算插件，提供基本数学计算和统计功能"
        }

# 插件实例
plugin_instance = MathCalculatorPlugin()

def register(plugin_manager):
    """注册插件到插件管理器"""
    plugin_manager.register_plugin('plugin_b', plugin_instance)
    print(f"数学计算插件已注册: {plugin_instance.name} v{plugin_instance.version}") 